package com.paiai.lslease.service.apprepairs;

import com.paiai.lslease.auto.dao.AppRepairsDAO;
import com.paiai.lslease.auto.model.AppRepairs;
import com.paiai.lslease.auto.model.AppRepairsExample;
import com.paiai.lslease.dao.apprepairs.AppRepairsExternalDAO;
import com.paiai.lslease.model.AppLandlordAuthHouseInfo;
import com.paiai.lslease.model.AppLockInfo;
import com.paiai.lslease.service.apphouse.AppLockService;
import com.paiai.lslease.service.landlordauth.AppLandlordAuthService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class AppRepairsServiceImpl implements AppRepairsService {

    private AppRepairsDAO appRepairsDAO;
    private AppRepairsExternalDAO appRepairsExternalDAO;
    @Autowired
    private AppLandlordAuthService appLandlordAuthService;
    @Autowired
    private AppLockService appLockService;

    public void setAppRepairsDAO(AppRepairsDAO appRepairsDAO) {
        this.appRepairsDAO = appRepairsDAO;
    }

    public void setAppRepairsExternalDAO(AppRepairsExternalDAO appRepairsExternalDAO) {
        this.appRepairsExternalDAO = appRepairsExternalDAO;
    }

    public AppRepairs repairsToLandlord(AppRepairs appRepairs) throws Exception {
        appRepairsDAO.insert(appRepairs);
        return appRepairsDAO.selectByPrimaryKey(appRepairsExternalDAO.getUpToDateAppRepairs());
    }

    public List<AppRepairs> selectRepairsListByLanlord(int landlordId) throws Exception {
        AppRepairsExample appRepairsExample = new AppRepairsExample();
        appRepairsExample.createCriteria().andLandlordIdEqualTo(landlordId).andItem1EqualTo("1");
        return appRepairsDAO.selectByExample(appRepairsExample);
    }

    public List<AppRepairs> selectRepairsListByTenant(int tenantId) throws Exception {
        AppRepairsExample appRepairsExample = new AppRepairsExample();
        appRepairsExample.createCriteria().andTenantIdEqualTo(tenantId).andItem1EqualTo("1");
        return appRepairsDAO.selectByExample(appRepairsExample);
    }

    @Override
    public List<AppRepairs> selectRepairsListByAuthManager(int authManagerId) throws Exception {
        //查询授权管理员管理的所有房源信息
        List<AppLandlordAuthHouseInfo> appLandlordAuthHouseInfoList = appLandlordAuthService.selectAppLandlordAuthListByUserId(authManagerId);
        if (appLandlordAuthHouseInfoList != null) {
            List<AppRepairs> appRepairsList = new ArrayList<AppRepairs>();
            for (AppLandlordAuthHouseInfo appLandlordAuthHouseInfo : appLandlordAuthHouseInfoList) {
                //找到所有房源的所有设备
                List<AppLockInfo> appLockList = appLockService.selectHouseAllLock(appLandlordAuthHouseInfo.getId());
                //找到设备的所有保修单
                if (appLockList != null) {
                   for (AppLockInfo appLockInfo : appLockList) {
                       AppRepairsExample appRepairsExample = new AppRepairsExample();
                       appRepairsExample.createCriteria().andLockIdEqualTo(appLockInfo.getId()).andItem1EqualTo("1");
                       List<AppRepairs> appRepairsList_ = appRepairsDAO.selectByExample(appRepairsExample);
                       if (appRepairsList_ != null) {
                           appRepairsList.addAll(appRepairsList_);
                       }
                   }
                }
            }
            return appRepairsList;
        }
        return null;
    }

    @Override
    public AppRepairs selectSingleRepairsInfoById(int repairsId) throws Exception {
        AppRepairs appRepairs = appRepairsDAO.selectByPrimaryKey(repairsId);
        return appRepairs !=null && "1".equalsIgnoreCase(appRepairs.getItem1()) ? appRepairs : null;
    }
}
